local n $n
local dataspec $dataspec
local prodspec $spec
local samplename $samplename


use "$workingnew/$dataspec", clear


xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
gen diff_tfpr = tfpr - L.tfpr

foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate ln_`var' = ln(`var')
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_tfpr diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}

** To get the final sample for all regs:
	ivreghdfe drop_term ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2#year) first cluster(nic_08_4dig)
			generate sample0 = e(sample)
			keep if sample0 == 1

foreach var in adding_term drop_term adding_term_h_j adding_term_a_j adding_term_d_j drop_term_h_j drop_term_a_j drop_term_d_j {
preserve
* Baseline IV 		
	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year) first cluster(nic_08_4dig)
				local b`n'_y = _b[diff_ln_IND]
				local se_b`n'_y = _se[diff_ln_IND]
				cap local I_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_y = _se[diff_wIPT_IND_CHN]
				cap local I_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_y = e(N)
				mat f = e(first)
				local F_b`n'_y = round(f[8,1], .01)
				cap local F_I_b`n'_y = round(f[8,2], .01)
				

	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year nic2) first cluster(nic_08_4dig)
				local b`n'_yn = _b[diff_ln_IND]
				local se_b`n'_yn = _se[diff_ln_IND]
				cap local I_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_yn = e(N)
				mat f = e(first)
				local F_b`n'_yn = round(f[8,1], .01)
				cap local F_I_b`n'_yn = round(f[8,2], .01)

	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(nic2#year) first cluster(nic_08_4dig)
				local b`n'_ycn = _b[diff_ln_IND]
				local se_b`n'_ycn = _se[diff_ln_IND]
				cap local I_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_ycn = e(N)
				mat f = e(first)
				local F_b`n'_ycn = round(f[8,1], .01)
				cap local F_I_b`n'_ycn = round(f[8,2], .01)

***
clear
set obs 1
gen bs = 0

gen `var'_b`n'_y = `b`n'_y'
cap gen `var'_I_b2_y = `I_b2_y'
cap gen `var'_I_b3_y = `I_b3_y'
gen `var'_N`n'_y = `N`n'_y'
gen `var'_F_b`n'_y = `F_b`n'_y'
cap gen `var'_F_I_b`n'_y = `F_I_b`n'_y'

gen `var'_b`n'_yn = `b`n'_yn'
cap gen `var'_I_b2_yn = `I_b2_yn'
cap gen `var'_I_b3_yn = `I_b3_yn'
gen `var'_N`n'_yn = `N`n'_yn'
gen `var'_F_b`n'_yn = `F_b`n'_yn'
cap gen `var'_F_I_b`n'_yn = `F_I_b`n'_yn'

gen `var'_b`n'_ycn = `b`n'_ycn'
cap gen `var'_I_b2_ycn = `I_b2_ycn'
cap gen `var'_I_b3_ycn = `I_b3_ycn'
gen `var'_N`n'_ycn = `N`n'_ycn'
gen `var'_F_b`n'_ycn = `F_b`n'_ycn'
cap gen `var'_F_I_b`n'_ycn = `F_I_b`n'_ycn'

save "$tempfold/bs`n'_`dataspec'_`var'", replace

*** SE
clear
set obs 1
gen bs = -1

gen `var'_b`n'_y = `se_b`n'_y'
cap gen `var'_I_b2_y = `se_I_b2_y'
cap gen `var'_I_b3_y = `se_I_b3_y'

gen `var'_b`n'_yn = `se_b`n'_yn'
cap gen `var'_I_b2_yn = `se_I_b2_yn'
cap gen `var'_I_b3_yn = `se_I_b3_yn'

gen `var'_b`n'_ycn = `se_b`n'_ycn'
cap gen `var'_I_b2_ycn = `se_I_b2_ycn'
cap gen `var'_I_b3_ycn = `se_I_b3_ycn'

save "$tempfold/bs`n'_`dataspec'_`var'_se", replace
restore
}

*** Compile results:
use "$tempfold/bs`n'_`dataspec'_adding_term_se", clear
foreach var in drop_term adding_term_h_j adding_term_a_j adding_term_d_j drop_term_h_j drop_term_a_j drop_term_d_j {
	merge 1:1 bs using "$tempfold/bs`n'_`dataspec'_`var'_se", assert(3) nogen
	}
save "$tempfold/bs`n'_`dataspec'_se", replace

use "$tempfold/bs`n'_`dataspec'_adding_term", clear
foreach var in drop_term adding_term_h_j adding_term_a_j adding_term_d_j drop_term_h_j drop_term_a_j drop_term_d_j {
	merge 1:1 bs using "$tempfold/bs`n'_`dataspec'_`var'", assert(3) nogen
	}
append using "$tempfold/bs`n'_`dataspec'_se"
order bs
save "$tempfold/bs`n'_`dataspec'", replace
